package com.j180.mybatisplus_2demo.CodeGenerator;

import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.po.TableFill;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import org.apache.commons.lang3.StringUtils;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class CodeGenerator {


    public static String scanner(String tip) {
        Scanner scanner = new Scanner(System.in);
        StringBuilder help = new StringBuilder();
        help.append("请输入" + tip + "：");
        System.out.println(help.toString());
        if (scanner.hasNext()) {
            String ipt = scanner.next();
            if (StringUtils.isNotEmpty(ipt)) {
                return ipt;
            }
        }
        throw new MybatisPlusException("请输入正确的" + tip + "！");
    }


    public static void main(String[] args) {

        // 代码生成器
        AutoGenerator mpg = new AutoGenerator();
      // 全局配置
        GlobalConfig gc = new GlobalConfig();
        //得到当前工作的目录
        String projectPath = System.getProperty("user.dir");
        //System.out.println(projectPath);
        //设置输出目录
        gc.setOutputDir(projectPath + "/mybatisplus_2demo/src/main/java");
        gc.setAuthor("ys");
        //设置代码生成的名字  %s 就是表的名字
        gc.setEntityName("%sEntity");
        gc.setMapperName("I%sDao");
        gc.setServiceName("I%sService");
        gc.setServiceImplName("%sServiceImpl");
        gc.setControllerName("%sCotroller");
        gc.setXmlName("%sMapper");
        gc.setOpen(false);
        gc.setIdType(IdType.AUTO);
        //文件覆盖
        gc.setFileOverride(true);
        gc.setDateType(DateType.ONLY_DATE);
        //gc.setSwagger2(true); //实体属性 Swagger2 注解
        mpg.setGlobalConfig(gc);

        // 数据源配置
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setUrl("jdbc:mysql://127.0.0.1:3308/j180_mp?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai&zeroDateTimeBehavior=CONVERT_TO_NULL&allowPublicKeyRetrieval=true");
        // dsc.setSchemaName("public");
        dsc.setDriverName("com.mysql.cj.jdbc.Driver");
        dsc.setUsername("root");
        dsc.setPassword("root");
        mpg.setDataSource(dsc);


        // 包配置 -----------------------------------
        PackageConfig pc = new PackageConfig();
        //设置父包
        pc.setModuleName(scanner("请输入模块名："));
        pc.setParent("mybatisCode");
        pc.setEntity("bean");
        pc.setMapper("dao");
        pc.setService("services");
        pc.setServiceImpl("services.impl");
        pc.setController("controller");
        pc.setXml("dao.mapper");
        mpg.setPackageInfo(pc);
        // 策略配置
        StrategyConfig strategy = new StrategyConfig();
        //去表前缀
        strategy.setTablePrefix("t_");
        //添加需要生产的的表
        strategy.setInclude("t_student","t_class");
        strategy.setNaming(NamingStrategy.underline_to_camel);
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);
        strategy.setEntityLombokModel(true);
        strategy.setRestControllerStyle(true);
        //设置逻辑删除的列名
        strategy.setLogicDeleteFieldName("deleted");
        //设置乐观锁的字段
        strategy.setVersionFieldName("version");
        //设置自动填充
        List<TableFill>  list= new ArrayList<>();
        TableFill insertFill = new TableFill("insert_time", FieldFill.INSERT);
        TableFill updateFill = new TableFill("update_time", FieldFill.INSERT_UPDATE);
        list.add(insertFill);
        list.add(updateFill);
        strategy.setTableFillList(list);
        mpg.setStrategy(strategy);

        mpg.execute();

    }
}
